use std::error::Error;
use std::path::Path;
use bytes::Bytes;
use rocksdb::DB;
use crate::Storage;

#[derive(Debug)]
pub struct RocksDbStorage(DB);

impl RocksDbStorage {
    fn new(path: impl AsRef<Path>) -> Self {
        RocksDbStorage(DB::open_default(path).unwrap())
    }
}


impl Storage for RocksDbStorage {
    fn get(&self, key: &str) -> Result<Option<Bytes>, Box<dyn Error>> {
        let v = self.0.get(key)?.unwrap();
        Ok(Some(v.into()))
    }

    fn set(&self, key: &str, value: Bytes) -> Result<Option<Bytes>, Box<dyn Error>> {
        self.0.put(key, value.clone()).unwrap();
        Ok(Some(value))
    }
}
